$(function () {

    setTaskInfo();
    selComment();
    selSessionUser();
});

//接受任务
function getTask() {
    if(user_id == 0 || user_id == "0" || user_id == null){
        alert("需要登录才能帮助ta！");
    }else{
        $.ajax({
            type: 'post',
            url: ctx + "/getTask",   //提交到的url
            cache: false,
            data: {"userid": user_id,"taskid": taskid},    //提交的参数
            dataType: '',
            success: function (data) {
                if(data.code == 0){
                    alert("任务接受失败！");
                }else {
                    alert("任务已接受，可以开始帮助ta了！");
                }
            },
            error: function (data) {
                alert("接受失败！");
            }
        });
    }
}


//用户头像的悬浮样式
function picOverStyle() {
    $("#myinfo").css("display","block");
}
//用户头像的悬浮样式
function picOutStyle() {
    $("#myinfo").css("display","none");
}


//设置任务信息
function setTaskInfo() {
    $("#task-type").text("[" + typename + "]");
    $("#task-title span").text(tasktitle);
    $("#help-title").text("帮帮："+tasktitle);
    $("#task-userpic").css("background-image", ctx + "/" + userpic);
    $("#task-nickname span").text(usernickname);
    $("#task-createtime span").text("发布时间：" + taskcreatetime.substr(0, 19));
    $("#task-cardnum span").text("card:" + taskcardnum);
    $("#task-starttime span").text("开始时间：" + taskstarttime.substr(0, 19));
    $("#task-endtime span").text("结束时间：" + taskendtime.substr(0, 19));
    $("#task-content").html(taskcontent);
    $("#comment-line-num").text("（已有评论" + taskcommentNum + "条评论）");
    $("#help-id").text(taskid);
}


//点击添加评论、回复
function addComment() {
    if(user_id == 0 || user_id == "0" || user_id == null){
        alert("需要登录才能评论！");
    }else{
        var c = $("#text-num").css("color");
        var text = $("#comment-content").val();
        var taskid = $("#help-id").text();
        var commentid = $("#comment-id").text();
        if (c == "rgb(150, 150, 150)" && text.length != 0) {
            $.ajax({
                type: 'post',
                url: ctx + "/addComment",   //提交到的url
                cache: false,
                data: {"userid": user_id,"taskid": taskid, "commentid": commentid,"text":text},    //提交的参数
                dataType: '',
                success: function (data) {
                    if(data.code == 0){
                        alert("评论失败！");
                    }else{
                        hideCommentBox();
                        $("#sel-comment-box").html("");
                        selComment();
                    }
                },
                error: function (data) {
                    alert("添加失败！");
                }
            });
        }
    }
}


//获取评论信息
function selComment() {
    var taskid = $("#help-id").text();
    $.ajax({
        type: 'post',
        url: ctx + "/selComment",   //提交到的url
        cache: false,
        data: {"taskid": taskid},    //提交的参数
        dataType: '',
        success: function (data) {
            // alert("查询成功！");
            var box = $("#sel-comment-box");
            $.each(data.list, function (index, item) {
                var comtop = "<div class=\"user-comment-box\">\n" +
                    "                        <div class=\"user-comment-commentid\" style=\"display: none\">" + item.id + "</div>\n" +
                    "                        <div class=\"user-comment-uesrid\" style=\"display: none\">" + item.user.id + "</div>\n" +
                    "                        <div class=\"user-comment-userpic\"></div>\n" +
                    "                        <div class=\"user-comment-top\">\n" +
                    "                            <span class=\"user-comment-nickname\">" + item.user.nickname + "：</span><span class=\"user-comment-content\">" + item.content + "</span>\n" +
                    "                        </div>";

                var commiddle = "";
                if (null != item.comment) {
                    commiddle = "<div class=\"user-comment-middle\">\n" +
                        "                            <span class=\"user-comment-middle-nickname\">@" + item.comment.user.nickname + "：</span><span\n" +
                        "                                class=\"user-comment-middle-content\">" + item.comment.content + "</span>\n" +
                        "                        </div>";
                }

                var combottom = "<div class=\"user-comment-bottom\">\n" +
                    "                            <div class=\"user-comment-time\">" + item.time.substr(0, 19) + "</div>\n" +
                    "                            <div class=\"user-comment-comment\" onclick=\"showCommentBox(this)\">回复</div>\n" +
                    "                            <div class=\"line\">|</div>\n" +
                    "                            <div class=\"user-comment-report\">举报</div>\n" +
                    "                        </div>\n" +
                    "                    </div>";

                box.append(comtop+commiddle+combottom);
                $("#user-comment-box:last").find("user-comment-userpic").css("background-image", ctx + "/" + item.user.pic);
            });
        },
        error: function (data) {
            alert("查询失败！");
        }
    });
}


//弹出评论弹出层
function showCommentBox(op) {
    var top = $(document).scrollTop();
    $(document).on('scroll.unable', function (e) {
        $(document).scrollTop(top);
    });

    var commentid = $(op).parents(".user-comment-box").find(".user-comment-commentid").text();
    var nickname = $(op).parents(".user-comment-box").find(".user-comment-nickname").text();
    var content = $(op).parents(".user-comment-box").find(".user-comment-content").text();
    var username = $(op).parents(".user-comment-box").find(".user-comment-nickname").text();
    $("#show-send-comment-box").css({"display": "block"});
    if (commentid <= 0 || null == commentid) {
        $("#help-title").css({"display": "block"});
        $("#user-title").css({"display": "none"});
        $("#comment-content").attr("placeholder", "发表评论");
    } else {
        $("#comment-id").text(commentid);
        $("#nickname").text(nickname);
        $("#content").text(content);
        $("#help-title").css({"display": "none"});
        $("#user-title").css({"display": "block"});
        $("#comment-content").attr("placeholder", "回复 " + username);
    }
    $("#comment-content").focus();
}

//关闭评论弹出层
function hideCommentBox() {
    $(document).unbind("scroll.unable");
    $("#comment-content").val("");
    $("#btn-comment").css({"cursor": "not-allowed"});
    $("#text-num").text(140);
    $("#text-num").css({"color": "rgb(150, 150, 150)"});
    $("#show-send-comment-box").css({"display": "none"});
}


//文本域输入计算数字
function countText() {
    var text = $("#comment-content").val();
    if (text.length >= 0) {
        if (text.length <= 140) {
            $("#btn-comment").css({"cursor": "pointer"});
            $("#text-num").text((140 - text.length));
            $("#text-num").css({"color": "rgb(150, 150, 150)"});
        } else {
            $("#btn-comment").css({"cursor": "not-allowed"});
            $("#text-num").text("-" + (text.length - 140));
            $("#text-num").css({"color": "red"});
        }
    } else {
        $("#btn-comment").css({"cursor": "not-allowed"});
        $("#text-num").text(140);
        $("#text-num").css({"color": "rgb(150, 150, 150)"});
    }
}


//点赞
function clickLike(op) {
    var url = $(op).find(".user-comment-like-img").css("background-image");
    if (url == "url('file:///E:/tdq/schoolhelp/img/like0.png')") {
        $(op).find(".user-comment-like-img").css("background-image", "url('file:///E:/tdq/schoolhelp/img/like1.png')");
    } else {
        $(op).find(".user-comment-like-img").css("background-image", "url('file:///E:/tdq/schoolhelp/img/like0.png')");
    }
}


//查询session中的用户
function selSessionUser() {
    $.ajax({
        type: 'post',
        url: ctx + "/selSessionUser",   //提交到的url
        cache: false,
        data: {},    //提交的参数
        dataType: '',
        success: function (data) {
            if (null == data || "" == data) {
                $("#login-regist-text").css("display","block");
                $("#mypic").css("display","none");
            }else {
                $("#login-regist-text").css("display","none");
                $("#mypic").css({"display":"block","background-image": "url(\""+ctx+"/"+data.pic+"\")"});
                $("#mynickname span").text(data.nickname);
                $("#myprofession span").text(data.profession);
                $("#mybalance span").text("余额："+data.balance);
                if(null != data.noreadnum && "" != data.noreadnum && 0 < Number(data.noreadnum) && 99 >= Number(data.noreadnum)){
                    $("#noread").css("display","block");
                    $("#noread").text(data.noreadnum);
                }else if(99 < Number(data.noreadnum)){
                    $("#noread").css("display","block");
                    $("#noread").text("99");
                }
            }
        },
        error: function (data) {
            alert("查询失败！");
        }
    });
}

[ctx + '/newrelease.jsp', 'no']
//iframe窗
function openIframe () {
    var index = layer.open({
        type: 2,
        content: [ctx + '/newrelease.jsp', 'no'],
        area: ['320px', '195px'],
        maxmin: true
    });
    layer.full(index);
}